/**
 * 
 */
package cn.ad.server.web.servlet.v2.requesthandler;

import cn.ad.server.web.servlet.v2.parser.DefaultClickRequestParser;
import cn.ad.server.web.servlet.v2.parser.HttpServletParser;
import cn.ad.server.web.servlet.v2.requests.ClickRequest;
import cn.ad.server.web.spring.model.IAdserverRequest;
import cn.ad.server.web.spring.model.IAdserverResponse;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;

/**
 * 广告点击请求处理handler
 * @author Wu Chengjun
 */
public class ClickRequestHandler extends TrackingRequestHandler {

	
	/**
	 * 
	 */
	public ClickRequestHandler() {
		super();
		defaultParser = new DefaultClickRequestParser();
	}

	/*
	 * (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.AdRequestHandler#handleResponse(javax.servlet.http.HttpServletResponse, cn.falconnect.adserver.adapter.HttpServletParser, cn.falconnect.adserver.adapter.IAdserverRequest, cn.falconnect.adserver.adapter.IAdserverResponse)
	 */
	@Override
	public void handleResponse(HttpServletResponse response, HttpServletParser servletParser, IAdserverRequest trackRequest, IAdserverResponse trackingResponse) throws Exception {
		if (trackRequest instanceof ClickRequest && StringUtils.isNotEmpty(((ClickRequest)trackRequest).getTarget())) {
			response.setStatus(302);
			response.setHeader("Location", URLDecoder.decode(((ClickRequest)trackRequest).getTarget(), "UTF-8"));
			response.setHeader("Connection", "close");
		}
	}
}
